package com.taxshare.purchaser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.constant.ResultConstant;
import com.taxshare.purchaser.entity.InvoiceGoodsRulesEntity;
import com.taxshare.purchaser.entity.InvoiceSellerRulesEntity;
import com.taxshare.purchaser.mapper.InvoiceGoodsRulesMapper;
import com.taxshare.purchaser.view.dto.InvoiceGoodsRulesDTO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: InvoiceGoodsRulesService
 * @Description:
 * @author: hjh
 * @date: 2021/7/16 11:12
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class InvoiceGoodsRulesService extends ServiceImpl<InvoiceGoodsRulesMapper, InvoiceGoodsRulesEntity> {

    /**
     * 更新商品明细规则（首次为添加）
     * @param bean
     * @return
     */
    public Response updateGoodsRules(InvoiceGoodsRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(StrUtil.hasBlank(bean.getGoodsNotPassReasons())){
            return Response.error(ResultConstant.RET_R0002_CODE, ResultConstant.RET_R0002_MSG);
        }
        if(StrUtil.hasBlank(bean.getGoodsSensitiveHead())){
            return Response.error(ResultConstant.RET_R0003_CODE, ResultConstant.RET_R0003_MSG);
        }
        //首次没有goodsRuleId，添加
        InvoiceGoodsRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceGoodsRulesEntity>().eq("uuid",bean.getUuid()));
        if(flg==null){
            InvoiceGoodsRulesEntity entity = DozerBeanUtil.map(bean, InvoiceGoodsRulesEntity.class);
            entity.setGoodsRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            int result = baseMapper.insert(entity);
            if(result!=1){
                return Response.error(ResultConstant.RET_D0001_CODE,ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        //如果有goodsRuleId，内容更新
        UpdateWrapper<InvoiceGoodsRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid", bean.getUuid())
                .set("goods_not_pass_reasons", bean.getGoodsNotPassReasons())
                .set("goods_sensitive_head", bean.getGoodsSensitiveHead())
                .set("goods_ticket_type", bean.getGoodsTicketType());
        InvoiceGoodsRulesEntity entity = new InvoiceGoodsRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE,ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }

    /**
     * 更新规则开启状态
     *
     * @param bean
     * @return
     */
    public Response updateGoodsIsOpen(InvoiceGoodsRulesDTO bean) {
        if (StrUtil.hasBlank(bean.getUuid())) {
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if (!CodeConstant.RULES_NOT_OPEN.equals(bean.getGoodsIsOpen()) && (!CodeConstant.RULES_OPEN.equals(bean.getGoodsIsOpen()))) {
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        InvoiceGoodsRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceGoodsRulesEntity>().eq("uuid", bean.getUuid()));
        if (flg == null) {
            InvoiceGoodsRulesEntity entity = new InvoiceGoodsRulesEntity();
            entity.setGoodsRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            entity.setGoodsIsOpen(bean.getGoodsIsOpen());
            int result = baseMapper.insert(entity);
            if (result != 1) {
                return Response.error(ResultConstant.RET_D0001_CODE, ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        UpdateWrapper<InvoiceGoodsRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid", bean.getUuid())
                .set("goods_is_open", bean.getGoodsIsOpen());
        InvoiceGoodsRulesEntity entity = new InvoiceGoodsRulesEntity();
        int result = baseMapper.update(entity, updateWrapper);
        if (result != 1) {
            return Response.error(ResultConstant.RET_D0002_CODE, ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }
}
